import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;


/*  展示各页面左栏里的用户信息  */
@WebServlet("/userInfo")
public class UserInfoServlet extends HttpServlet {
    ObjectMapper objectMapper=new ObjectMapper();

    /*  列表页 + 详情页  */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String blogId=req.getParameter("blogId");
        //有blogId的是博客详情页，没有的是博客列表页
        if(blogId==null){
            //列表页获取 当前登录用户的信息
            //直接从sessionz中获取
            getUserInfoFromSession(req,resp);
            
        }else{
            //详情页，获取文章作者信息
            //查数据库获取
            try {

                getUserInfoFromDB(req,resp,Integer.parseInt(blogId));

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        


    }

    private void getUserInfoFromDB(HttpServletRequest req, HttpServletResponse resp, int blogId) throws SQLException, IOException {
        //blogId -> Blog对象 -> userId
        //userId -> User对象
        BlogDao blogDao=new BlogDao();
        Blog blog=blogDao.selectOne(blogId);
        if(blog==null){
            //如果传过来的blogId的瞎写的，数据库里没有 Blog对象为空
            resp.setStatus(404);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("blogId不存在");
            return;
        }

        //根据blogId查询对应的User对象
        UserDao userDao=new UserDao();
        User user=userDao.selectById(blog.getUserId());
        if(user==null){
            resp.setStatus(404);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("blogId不存在");
            return;

        }

        user.setPassword("");
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));

    }

    private void getUserInfoFromSession(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session= req.getSession(false);
        if(session==null){
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录");
            return;
        }

        User user=(User)session.getAttribute("user");
        if(user==null){
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("当前未登录");
            return;
        }

        //user获取到了，把user中的password干掉，然后返回
        user.setPassword("");
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(objectMapper.writeValueAsString(user));
    }


}
